package com.jss.lanouhn.fristapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.HomeItemDetail;
import com.jss.lanouhn.fristapp.modles.HomeAuthorContentInfo;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lanouhn on 16/9/1.
 */
public class AuthorRVContentAdapter extends RecyclerView.Adapter<AuthorRVContentAdapter.RvContentViewHolder>{
    private Context mContent;
    private HomeAuthorContentInfo contentInfo;


    public AuthorRVContentAdapter(Context mContent, HomeAuthorContentInfo contentInfo) {
        this.mContent = mContent;
        this.contentInfo = contentInfo;
    }

    @Override
    public RvContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContent).inflate(R.layout.item_rv_home_author,null);
        RvContentViewHolder holder=new RvContentViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RvContentViewHolder holder, int position) {
        final HomeAuthorContentInfo.DataBean.PostsBean bean=contentInfo.getData().getPosts().get(position);
        Picasso.with(mContent).load(bean.getCover_image_url()).into(holder.iv_item_author);
        holder.tv_item_author.setText(bean.getTitle());
        holder.tv_item_times.setText(new SimpleDateFormat("MM-dd").format(new Date(bean.getCreated_at()*1000L))+"");
        holder.tv_item_author_likes.setText(bean.getLikes_count()+"");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContent, HomeItemDetail.class);
                intent.putExtra("id", "" + bean.getId());
                mContent.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return contentInfo.getData().getPosts().size();
    }

    public class RvContentViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv_item_author;
        private TextView tv_item_author,tv_item_times,tv_item_author_likes;
        public RvContentViewHolder(View itemView) {
            super(itemView);

            iv_item_author= (ImageView) itemView.findViewById(R.id.iv_item_author);
            tv_item_author= (TextView) itemView.findViewById(R.id.tv_item_author);
            tv_item_times= (TextView) itemView.findViewById(R.id.tv_item_times);
            tv_item_author_likes= (TextView) itemView.findViewById(R.id.tv_item_author_likes);
        }
    }
}
